package com.example.spring8.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.spring8.model.User;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class UserController {
    @GetMapping("/add")
    public String insertUser(HttpServletRequest request, HttpServletResponse response, Model model){
        request.setAttribute("username","张三");
        model.addAttribute("username","张三");
        System.out.println("get新增了一个用户");
        return "user/userAdd";
    }
    @PostMapping("/add")
    public String insertUser1(Model model){
        model.addAttribute("username","李四");
        System.out.println("post新增了一个用户");
        return "user/userAdd";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST,params = {"name"},headers = {"token"})
    public String updatetUser(String name){
        System.out.println("修改了一个用户");
        return "user/userUpdate";
    }
    @PostMapping("/list")
    @ResponseBody
    public String list(){
        List<String> list=new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        return "张三";
    }
    @PostMapping("/list1")
    @ResponseBody
    public JSONObject list1(@RequestParam(value = "name" ) String name1, @RequestParam("age") String age1){
        System.out.println(name1);
        System.out.println(age1);
        List<String> stringList=new ArrayList<>();
        stringList.add("张三");
        stringList.add("李四");
        stringList.add("王五");

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","操作成功");
        jsonObject.put("list",stringList);
        return jsonObject;
    }

    @PostMapping("/path/{id}/{name}")
    @ResponseBody
    public Integer path(@PathVariable("id") int id,@PathVariable("name") String name){
        System.out.println(id);
        return id;
    }
    @PostMapping("/param")
    @ResponseBody
    public String param( User user){
        System.out.println(user.getUsername());
        return user.getNickname();
    }
    @PostMapping("/arrayList")
    @ResponseBody
    public String array(String[] stringList){
        for (String str:stringList) {
            System.out.println(str);
        }
        return stringList[0];
    }
    @PostMapping("/date")
    @ResponseBody
    public String date(@RequestBody User user){
        System.out.println(user.getCreateTime());
        return user.getCreateTime()+"";
    }

    @GetMapping("/session")
    public String session(HttpServletRequest request, Model model, SessionStatus status){
        HttpSession session = request.getSession();
        System.out.println(model.getAttribute("username"));
        System.out.println(session.getAttribute("username"));
        status.setComplete();
        System.out.println(session.getAttribute("username"));
        return "user/userAdd";
    }

}
